package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessReward;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.RewardVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.RewardHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/5/17 16:10
 * 修改备注：
 * ================================================
 */
public class RewardActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    @Bind(R.id.tv_my_coin)
    TextView tvMyCoin;
    @Bind(R.id.edit_coin)
    EditText editCoin;
    @Bind(R.id.btn_reward)
    Button btnReward;

    String coinNmu = "";
    String redType = "";
    String agriConsultId = "";

    private RecyclerArrayAdapter adapter;
    private List<RewardVO> list = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_reward);
        setTitle("打赏");
    }

    @Override
    public void initMember() {

        Map<String, String> map = (Map<String, String>) getIntent().getSerializableExtra("map");
        if (map == null)
            return;

        redType = map.get("redType");
        agriConsultId = map.get("agriConsultId");

        BusinessAccount.getCoinPointList(this, String.valueOf(1), String.valueOf(1), "0", mHandler);

        list.add(new RewardVO("50", "50农币", false));
        list.add(new RewardVO("100", "100农币", false));
        list.add(new RewardVO("150", "150农币", false));
        list.add(new RewardVO("200", "200农币", false));
        list.add(new RewardVO("250", "250农币", false));
        list.add(new RewardVO("300", "300农币", false));

        adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new RewardHolder(parent);
            }
        };

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, true, true, false));
        recyclerView.setAdapter(adapter);


        adapter.clear();
        adapter.addAll(list);
    }

    @Override
    public void addListener() {

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                editCoin.setText("");
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).setSelected(true);
                        coinNmu = list.get(i).getCoin();
                    } else
                        list.get(i).setSelected(false);
                }
                adapter.notifyDataSetChanged();
            }
        });


        editCoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                coinNmu = "";
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setSelected(false);
                }
                adapter.notifyDataSetChanged();
            }
        });


        btnReward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isEditEmpty(editCoin) && StringUtil.isStrEmpty(coinNmu)) {
                    showToast("请输入或选择金额");
                    return;
                }

                String money = "";

                if (!StringUtil.isStrEmpty(coinNmu))
                    money = coinNmu;
                else
                    money = editCoin.getText().toString();

                BusinessReward.reward(RewardActivity.this, money, agriConsultId, redType, mHandler);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_COIN_POINT_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_REWARD_SUCCESS:
                        new MaterialDialog.Builder(RewardActivity.this)
                                .title("提示")
                                .content("已打赏成功，谢谢支持！")
                                .positiveText("确定")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        setResult(RESULT_OK);
                                        finish();
                                    }
                                })
                                .show();
                        break;
                    case MSG.MSG_REWARD_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        String num = FastJsonUtil.getString(object.toString(), "pointTotalValue");
        tvMyCoin.setText("我的农币：" + num + "农币");
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
